package com.chinasoft04.mall.service.impl;

import com.chinasoft04.mall.mapper.CategoryMapper;
import com.chinasoft04.mall.service.CategoryService;
import com.chinasoft04.mall.dto.Category;
import com.github.pagehelper.PageHelper;
import lombok.AllArgsConstructor;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@AllArgsConstructor
public class CategoryServiceImpl implements CategoryService {

    CategoryMapper categoryMapper;

    @Override
    public Object getCategories(Integer type, @Nullable Integer pagenum, @Nullable Integer pagesize) {
        Map<String, Object> map = new HashMap<>();
        List<Category> categories = categoryMapper.selectAll(0, 0);
        int total = categoryMapper.selectA(0, 0);
        if (pagenum != null && pagesize != null) {
            PageHelper.startPage(pagenum, pagesize);
            categories = categoryMapper.selectAll(0, 0);
            map.put("total", total);
            map.put("pageNum", pagenum);
            map.put("pageSize", pagesize);
        }
        switch (type) {
            case 1:
                if (pagenum != null && pagesize != null) {
                    map.put("result", categories);
                    return map;
                }
                return categories;
            case 2:
                List<Category> catgoriesChil1 = getCatgoriesChil1(categories, 1);
                if (pagenum != null && pagesize != null) {
                    map.put("result", catgoriesChil1);
                    return map;
                }
                return catgoriesChil1;
            case 3:
                List<Category> catgoriesChil2 = getCatgoriesChil2(categories, 2);
                if (pagenum != null && pagesize != null) {
                    map.put("result", catgoriesChil2);
                    return map;
                }
                return catgoriesChil2;
            default:
                break;
        }
        return null;
    }

    @Override
    public Category insertCategory(Category category) {
        int i = categoryMapper.insertOne(category);
        if (i == 1) {
            Category category1 = categoryMapper.selectById(category.getCat_id());
            return category1;
        } else {
            return null;
        }
    }

    @Override
    public Category getOneCategory(Integer id) {
        return categoryMapper.selectById(id);
    }

    @Override
    public Category updateCategory(Integer id, String cat_name) {
        int flag = categoryMapper.updateById(id, cat_name);
        if (flag == 1) {
            Category category = categoryMapper.selectById(id);
            return category;
        }
        return null;
    }

    @Override
    public int deleteCategory(Integer id) {
        return categoryMapper.deleteById(id);
    }

    public List<Category> getCatgoriesChil1(List<Category> categories, Integer type) {
        for (Category i : categories) {
            List<Category> childrens = categoryMapper.selectAll(type, i.getCat_id());
            i.setChildren(childrens);
        }
        return categories;
    }

    public List<Category> getCatgoriesChil2(List<Category> categories, Integer type) {
        for (Category i : categories) {
            List<Category> childrens1 = categoryMapper.selectAll(type - 1, i.getCat_id());
            i.setChildren(childrens1);
            for (Category j : childrens1) {
                List<Category> childrens2 = categoryMapper.selectAll(type, j.getCat_id());
                j.setChildren(childrens2);
            }
        }
        return categories;
    }
}
